package com.lanou.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.bean.AddrInfo;
import com.lanou.bean.City;
import com.lanou.bean.Province;
import com.lanou.bean.VipInfo;
import com.lanou.mapper.AddrInfoMapper;
import com.lanou.service.VipInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员信息管理
 */

@Controller
@RequestMapping("/vip")
public class VipInfoController {

    @Autowired
    private VipInfoService vipInfoService;
    @Autowired
    private AddrInfoMapper addrInfoMapper;

    //查询所有的会员信息,进行数据的展示
    @RequestMapping(value = "/selectAllVipInfo")
    public ModelAndView selectAllVipInfo(VipInfo vipInfo, Integer currentPage,Map<String,Object> map) {
        System.out.println("前端接收的VIPInfo信息------"+vipInfo);

        ModelAndView modelAndView = new ModelAndView("vip/findVipInfo");
        //查询所有会员信息
        //分页的信息
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        //查询所有的会员信息
        List<VipInfo> vipInfoList1 = vipInfoService.selectAllVipInfo1(vipInfo);
        for (VipInfo viplist:vipInfoList1
             ) {
            viplist.setProvince(vipInfoService.selectProvinceByAddrId(viplist.getAddrId()));
            viplist.setCity(vipInfoService.selectCityByAddrId(viplist.getAddrId()));
        }
        modelAndView.addObject("vip1",vipInfoList1);
        PageHelper.startPage(currentPage, 2);

        List<VipInfo> vipInfoList = vipInfoService.selectAllVipInfo2(vipInfo);

        System.out.println("获取的会员信息-----"+vipInfoList);
        PageInfo<VipInfo> pageInfo = new PageInfo<>(vipInfoList);
        modelAndView.addObject("page", pageInfo);

        //查询所有的省信息
        List<Province> provinceList = vipInfoService.selectAllProvince();
        modelAndView.addObject("provinceList", provinceList);
        return modelAndView;
    }


    //根据省的id进行市信息的回显
    @RequestMapping(value = "/findCity.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<City> selectAllCityByProvinceId(Integer provinceId) {
        List<City> cityList = vipInfoService.selectAllCityByProvinceId(provinceId);
        return cityList;
    }

    //进行修改的时候数据的回显(将信息回显)
    @RequestMapping("/showVipInfo.do")
    public String showVipInfo(Map<String,Object> map, int vipId, String vipNo, String vipAccount, int vipLevel, String vipType, String email, String phoneNo1,
                              String phoneNo2, String idCard, String realName, String regDate) throws UnsupportedEncodingException, ParseException {
        //将接收的会员信息封装到对象中
        VipInfo vipInfo = new VipInfo();
        vipInfo.setId(vipId);
        vipInfo.setVipNo(vipNo);
        vipInfo.setVipAccount(vipAccount);
        vipInfo.setVipLevel(vipLevel);
        vipInfo.setVipType(vipType);
        vipInfo.setEmail(email);
        vipInfo.setPhoneNo1(phoneNo1);
        vipInfo.setPhoneNo2(phoneNo2);
        System.out.println(regDate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(regDate);
        vipInfo.setRegDate(date);
        vipInfo.setIdCard(idCard);
        realName = new String(realName.getBytes("iso-8859-1"), "utf-8");
        vipInfo.setRealName(realName);
        System.out.println("前端传过来的会员信息--------"+vipInfo);
        //执行更新方法
        map.put("map", vipInfo);
        return "vip/updateVipInfo";
    }


    //获取会员信息界面修改后的信息,进行更新
    @RequestMapping("/updateVipInfo.do")
    @ResponseBody
    public String updateVipInfo(int id, String idCard,
                                String realName, String email, String phoneNo1, String phoneNo2,
                                String vipType, int vipLevel) {
        //接收会员更新后的信息,封装到对象中
        VipInfo vipInfo = new VipInfo();
        vipInfo.setId(id);
        vipInfo.setIdCard(idCard);
        vipInfo.setRealName(realName);
        vipInfo.setEmail(email);
        vipInfo.setPhoneNo1(phoneNo1);
        vipInfo.setPhoneNo2(phoneNo2);
        vipInfo.setVipType(vipType);
        vipInfo.setVipLevel(vipLevel);
        System.out.println("更新的会员信息-------"+vipInfo);
        //执行更新的方法
        int result = vipInfoService.updateByIdSelective(vipInfo);
        if (result == 1) {
            return "success";
        }
        return "false";
    }

    //地址修改中的回显
    @RequestMapping("/showSite.do")
    public String showSite(Map<String, Object> map,String cityId, String provinceId, String vipNo, String addId) throws UnsupportedEncodingException {

        //将接收的数据进行转码
        cityId = new String(cityId.getBytes("iso-8859-1"), "utf-8");
        provinceId = new String(provinceId.getBytes("iso-8859-1"), "utf-8");
        vipNo = new String(vipNo.getBytes("iso-8859-1"), "utf-8");
        addId = new String(addId.getBytes("iso-8859-1"), "utf-8");
        System.out.println("获取的会员信息------"+vipNo+"----详细地址:"+addId);
        //根据vipNo获取addrInfo的id信息
        int addrid = addrInfoMapper.selectAddrIdByVipNo(vipNo);
        System.out.println("获取到的地址id信息-----"+addrid);

        //将地址信息封装到VIPInfo中
        VipInfo vipInfo = new VipInfo();
        vipInfo.setProvince(vipInfoService.selectProvinceByVipNo(addrid));
        System.out.println("省信息------"+vipInfoService.selectProvinceByVipNo(addrid));

        vipInfo.setVipNo(vipNo);

        vipInfo.setCity(vipInfoService.selectCityByVipNo(addrid));
        System.out.println("市信息------"+vipInfoService.selectCityByVipNo(addrid));
        vipInfo.setAddrInfo(addId);
        vipInfo.setId(addrid);
        System.out.println("地址信息更新------"+vipInfo);
        //将数据放到域对象中
        map.put("list", vipInfo);

        //查询所有的省信息,进行省市联动
        List<Province> provinceList = vipInfoService.selectAllProvince();
        map.put("provinceList", provinceList);
        return "vip/findSite";
    }


    //获取详细地址页面信息,进行更新操作
    @RequestMapping("/updateSite.do")
    @ResponseBody
    public String updateSite(String provinceId, String cityId, String addrInfo, String addId) {
        AddrInfo addrInfo1 = new AddrInfo();
        addrInfo1.setId(Integer.parseInt(addId));
        addrInfo1.setAddrInfo(addrInfo);
        addrInfo1.setCityId(Integer.parseInt(cityId));
        addrInfo1.setProvinceId(Integer.parseInt(provinceId));
        //更新会员地址信息,更新addr_info
        int result = vipInfoService.updateAddrInfo(addrInfo1);
        if (result == 1) {
            return "success";
        }
        return "false";
    }


    /**
     *  会员信息的更新(返回int类型)
     * @return
     */
    @RequestMapping("/updateByIdSelective.do")
    @ResponseBody
    public Map<String, Object> updateVipInfo(VipInfo vipInfo){
        System.out.println("接收到的更新会员信息---"+vipInfo);
        int result = vipInfoService.updateByIdSelective(vipInfo);
        Map<String,Object> map=new HashMap<>();
        if(result>0){
           map.put("result","success");
           return map;
        }else {
            map.put("result","false");
            return map;
        }

    }
}
